package ca.lscs.web;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import ca.lscs.dal.ChecklistDao;
import ca.lscs.dal.DuplicateUsernameException;
import ca.lscs.dal.UserDao;
import ca.lscs.model.AppUser;
import ca.lscs.model.ChecklistGroup;
import ca.lscs.model.ChecklistItem;
import ca.lscs.model.UserType;

/**
 * @author Steve Pennington
 *
 */
@Controller
public class AdminController {

	@RequestMapping(value="/admin/checklistitem/create", method=RequestMethod.GET)
	public ModelAndView handleCreateCheckListItem() {
		ChecklistDao checklistDao = ChecklistDao.getInstance();
		
		List<ChecklistGroup> groupsToCreate = new ArrayList<ChecklistGroup>();
		
		ChecklistGroup planTitle = new ChecklistGroup("Plan Title", 1);
		groupsToCreate.add(planTitle);
		ChecklistGroup mainPlan = new ChecklistGroup("Main Body of Plan", 2);
		groupsToCreate.add(mainPlan);
		ChecklistGroup scenery = new ChecklistGroup("Scenery", 3);
		groupsToCreate.add(scenery);
		ChecklistGroup deposit = new ChecklistGroup("Deposit Statement", 4);
		groupsToCreate.add(deposit);
		ChecklistGroup integrated = new ChecklistGroup("Integrated Survey Area", 5);
		groupsToCreate.add(integrated);
		ChecklistGroup misc = new ChecklistGroup("Miscellaneous", 6);
		groupsToCreate.add(misc);
		ChecklistGroup electronic = new ChecklistGroup("Electronic Plan", 7);
		groupsToCreate.add(electronic);
		
		checklistDao.putGroups(groupsToCreate);
		
		List<ChecklistItem> itemsToCreate = new ArrayList<ChecklistItem>();
		
		itemsToCreate.add(new ChecklistItem("Type of Plan", 1, planTitle));
		itemsToCreate.add(new ChecklistItem("Legal Description & registered plan no.", 2, planTitle));
		itemsToCreate.add(new ChecklistItem("BCGS NO", 3, planTitle));
		itemsToCreate.add(new ChecklistItem("Appropriate Scale & Bar, including intended plot size", 4, planTitle));
		itemsToCreate.add(new ChecklistItem("Legend explaining all symbols and non-standard abbreviations", 5, planTitle));
		itemsToCreate.add(new ChecklistItem("Bearing derivation and reference", 6, planTitle));
		itemsToCreate.add(new ChecklistItem("Notation: bearings to BTs are magnetic or planned bearings", 7, planTitle));
		itemsToCreate.add(new ChecklistItem("North Point", 8, planTitle));
		
		itemsToCreate.add(new ChecklistItem("Appropriate designation for title or Interest parcels (ex Lot Number)", 1, mainPlan));
		itemsToCreate.add(new ChecklistItem("All essential dimensions given and closure calculated", 2, mainPlan));
		itemsToCreate.add(new ChecklistItem("Title & Interest Parcel Area or Volume correct & to required precision-GSI Rule 3", 3, mainPlan));
		itemsToCreate.add(new ChecklistItem("Boundaries reestablished and/or lots divided in accordance with Land Survey Act", 4, mainPlan));
		itemsToCreate.add(new ChecklistItem("Sufficient ties to evidence of previous surveys", 5, mainPlan));
		itemsToCreate.add(new ChecklistItem("Monumentation labeled and correct - GSI Rule 1-2 to 1-7", 6, mainPlan));
		itemsToCreate.add(new ChecklistItem("Read or �Lane� and name, when available, where road is being dedicated", 7, mainPlan));
		itemsToCreate.add(new ChecklistItem("Remember to check for hooked parcels, part parcels and remainders", 8, mainPlan));
		itemsToCreate.add(new ChecklistItem("New Dedicated Road or RW fully dimensioned with widths indicated-GSI Rule", 9, mainPlan));
		itemsToCreate.add(new ChecklistItem("No text less than 2mm", 10, mainPlan));
		itemsToCreate.add(new ChecklistItem("Plotting to scale and drafting legible - GSI Rule 3-2 & 3-3", 11, mainPlan));
		itemsToCreate.add(new ChecklistItem("Bold outline 1.0 - 1.5 mm centered on boundary (including any detail drawings)", 12, mainPlan));
		itemsToCreate.add(new ChecklistItem("Existing R/W, Easement or Covenant boundaries shown with broken lines - GSI Rule 3-4", 13, mainPlan));
		itemsToCreate.add(new ChecklistItem("Details of bearing trees and ancillary evidence found and made - GSI Rule 3-4", 14, mainPlan));
		itemsToCreate.add(new ChecklistItem("Radius, arc, radial bearings for each curve point - GSI Rule 3-4", 15, mainPlan));
		itemsToCreate.add(new ChecklistItem("Railway plan in un-surveyed land has district lot number assigned", 16, mainPlan));
		itemsToCreate.add(new ChecklistItem("Access to water body where applicable - LTA s75(1)", 17, mainPlan));
		itemsToCreate.add(new ChecklistItem("Label Un-surveyed Crown Land including theoretical or unsurveyed portions of townships", 18, mainPlan));
		
		itemsToCreate.add(new ChecklistItem("Check status of adjacent roads. Have they all been dedicated?", 1, scenery));
		itemsToCreate.add(new ChecklistItem("Parcel boundaries (incl. highway, roads and railway) shown with solid lines - Rule 3-4(2)(g)", 2, scenery));
		itemsToCreate.add(new ChecklistItem("Description(s) given for all surrounding lands - GSI Rule 3-4(1)(r)", 3, scenery));
		itemsToCreate.add(new ChecklistItem("Primary parcel designations prominent in body of plan (use 'DL' not 'Lot') - Rule 10-14", 4, scenery));
		itemsToCreate.add(new ChecklistItem("Existing Road Names shown - GSI Rule 3-", 5, scenery));
		itemsToCreate.add(new ChecklistItem("Roads, Trails, and Seismic Lines shown and labeled with width and posted as required", 6, scenery));
		itemsToCreate.add(new ChecklistItem("\'Rem\' added on lot and \"portion of\" or \"part of\" in title where appropriate", 7, scenery));
		
		itemsToCreate.add(new ChecklistItem("Plan lies within (Regional District) statement - GSI Rule 3-4", 1, deposit));
		itemsToCreate.add(new ChecklistItem("Leave 7 cm 12 cm clear space in top right corner for Registrar's notation pursuant to S 56 LTA", 2, deposit));
		
		itemsToCreate.add(new ChecklistItem("Grid bearing notation; ISA name and number, datum and bearing derivation - GSI Rule 5-7", 1, integrated));
		itemsToCreate.add(new ChecklistItem("Control monuments tied in accordance with GSI Rules 5-4(2)", 2, integrated));
		itemsToCreate.add(new ChecklistItem("Meets accuracy standards of integrated legal survey - GSI Rule 5-4 (3) & (4)", 3, integrated));
		itemsToCreate.add(new ChecklistItem("Control monuments shown on plan with required symbol and respective designation - GSI Rule 5-7(2)", 4, integrated));
		
		itemsToCreate.add(new ChecklistItem("Spelling check", 1, misc));
		itemsToCreate.add(new ChecklistItem("Standard plan size - GSI Rule 3-1", 2, misc));
		itemsToCreate.add(new ChecklistItem("If practical, top of plan orientated north - GSI Rule 3-3(5)", 3, misc));
		itemsToCreate.add(new ChecklistItem("Notation regarding existing records that plan is compiled from.", 4, misc));
		
		itemsToCreate.add(new ChecklistItem("Plan Image created with Adobe 6.0 or higher with minimum 600 dpi resolution - GSI Rule 3-1 (1)", 1, electronic));
		itemsToCreate.add(new ChecklistItem("All plan features black ink on white background with no ornate fonts - GSI Rule 3-3(1)", 2, electronic));
		itemsToCreate.add(new ChecklistItem("No signatures on plan - GSI Rule 3-3(7)", 3, electronic));
		itemsToCreate.add(new ChecklistItem("Plan complies with all standards for electronic submissions approved by S.G. GSI Rule 3-3 (12)", 4, electronic));
		
		checklistDao.putItems(itemsToCreate);
		
		return new ModelAndView("createchecklistitem");
	}
	
	@RequestMapping(value="/admin/user", method=RequestMethod.GET)
	public ModelAndView handleCreateUser() {
		return new ModelAndView("createuser");
	}
	
	@RequestMapping(value="/admin/user", method=RequestMethod.POST)
	public ModelAndView doCreateUser(HttpServletRequest request) {
		try {
			String username = ServletRequestUtils.getRequiredStringParameter(request, "username");
			String firstName = ServletRequestUtils.getRequiredStringParameter(request, "firstname");
			String lastName = ServletRequestUtils.getRequiredStringParameter(request, "lastname");
			String password = ServletRequestUtils.getRequiredStringParameter(request, "password");
			String type = ServletRequestUtils.getRequiredStringParameter(request, "type");
			
			AppUser user = new AppUser(username, firstName, lastName, password, UserType.valueOf(type));
			UserDao.getInstance().put(user);
		} catch (ServletRequestBindingException e) {
			//Invalid params
		} catch(IllegalArgumentException e) {
			//Invalid type
		} catch (DuplicateUsernameException e) {
			//duplicate username
		} catch(NoSuchAlgorithmException e) {
			//Hashing error
		} catch(IOException e) {
			//Hashing error
		}
		
		return new ModelAndView("createuser");
	}
	
	@RequestMapping(value="/admin", method=RequestMethod.GET)
	public ModelAndView handleAdmin() {
		return new ModelAndView("admin");
	}
}
